package net.mcg_server.jbi.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.mcg_server.jbi.Program;
import net.mcg_server.jbi.data.Game;

@SuppressWarnings("serial")
public class MainPanel extends JPanel {

	private String text = new String(System.getProperty("java.version"));
	private JTable gameTable;
	private DefaultTableModel model;
	private Program program;

	public MainPanel(Program program) {
		this.program = program;
		
		this.setLayout(null);

		JLabel x1 = new JLabel("This application requires Java version 1.7.x to be installed. \n");
		this.add(x1);

		JLabel x2 = new JLabel("The version of Java being used is " + text.substring(0, 5));
		this.add(x2);

		JLabel title = new JLabel("Game List:");
		this.add(title);


		// TABLE -------------

		gameTable = new JTable();

		Object[] columnNames = {"Game", "Description", "Beaten?"};

		model = new DefaultTableModel(null, columnNames);
		gameTable.setModel(model);
		//gameTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		gameTable.getColumnModel().getColumn(0).setPreferredWidth(100);
		gameTable.getColumnModel().getColumn(1).setPreferredWidth(400);
		gameTable.getColumnModel().getColumn(2).setPreferredWidth(75);

		JScrollPane scrollArea = new JScrollPane(gameTable);
		scrollArea.setBackground(Color.WHITE);

		this.add(scrollArea);

		// END TABLE -----------


		Insets inset = this.getInsets();
		Dimension size = new Dimension();

		size = x1.getPreferredSize();
		x1.setBounds(175+inset.left, 20+inset.top, size.width, size.height);

		size = x2.getPreferredSize();
		x2.setBounds(175+inset.left, 40+inset.top, size.width, size.height);

		size = title.getPreferredSize();
		title.setBounds(30+inset.left, 70+inset.top, size.width, size.height);

		scrollArea.setBounds(30+inset.left, 85+inset.top, 575, 300);
		
		this.loadData();
		
	}

	public void loadData(){

		//clear the table
		if(model.getRowCount() > 0){
			for(int j = model.getRowCount()-1; j >= 0; j--){
				model.removeRow(j);
				//System.out.println(j);
			}
		}

		//populate the table
		for(int i = 0; i < program.getGameList().size(); i++){
			// code to fill in rows from the Program.games array
			Game game = program.getGameList().get(i);
			
			String title = game.getTitle();
			String description = game.getDescription();
			Boolean beaten = game.isBeaten();
			
			String isBeat = new String();
			
			if(beaten == true){
				isBeat = "Yes";
			}else
				isBeat = "No";
			
			String[] aRow = {title, description, isBeat};
			
			model.addRow(aRow);
		}
	}

}
